package linked;/*
        问题描述：
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
使用双指针，然后处理剩余部分。

输入：l1 = [1,2,4], l2 = [1,3,4]
输出：[1,1,2,3,4,4]

输入：l1 = [], l2 = []
输出：[]

输入：l1 = [], l2 = [0]
输出：[0]

 */

public class MergeTwoArray {
    public static void main(String[] args) {
        int[] l1 = {1, 2, 4, 4, 6, 8, 12}, l2 = {1, 3, 4};
        int[] ints = mergeTwoLists(l1, l2);
        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }

    public static int[] mergeTwoLists(int[] l1, int[] l2) {
        int len1 = l1.length;
        int len2 = l2.length;
        int[] res = new int[len1 + len2];
        int i = 0;
        int j = 0;
        int k = 0;
        while (i < len1 && j < len2) {
            if (l1[i] <= l2[j]) {
                res[k++] = l1[i++];
            }else {
                res[k++] = l2[j++];
            }
        }
        while (i < len1) {
            res[k++] = l1[i++];
        }
        while (j < len2) {
            res[k++] = l2[j++];
        }
        return res;
    }
}
